package com.mentaleye.controller;

import com.alibaba.fastjson.JSONObject;
import com.mentaleye.bean.UserLoginBean;
import com.mentaleye.biz.*;
import com.mentaleye.dal.service.AssessmentAnswerMapper;
import com.mentaleye.enums.CommonStatus;
import com.mentaleye.enums.DictionaryEnums;
import com.mentaleye.shiro.Constants;
import com.mentaleye.shiro.RedisCache;
import com.mentaleye.shiro.RedisKeyEnum;
import com.mentaleye.util.CommonPage;
import org.apache.log4j.Logger;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.Map;

/**
 * 填写 和结果查看
 */

@Controller
@RequestMapping({"/assessmentAnswer"})
public class AssessmentAnswerController {
    @Autowired
    private RedisCache<String, Object> redisCache;

    private static Logger logger = Logger.getLogger(AssessmentAnswerController.class);
    @Autowired
    private DictionaryBiz dictionaryBiz;
    @Autowired private OaClassBiz oaClassBiz;
    @Autowired
    private AutoFormBiz autoFormBiz;
    @Autowired
    TeacherBiz teacherBiz;

    @Autowired
    private AssessmentAnswerMapper      assessmentAnswerMapper;
    //获取 需要填写或者已经填写的列表
    @RequestMapping(value = "/formList/{assessmentType}")
    public ModelAndView list(@PathVariable("assessmentType") Integer assessmentType) {
        ModelAndView view = new ModelAndView("cm/assessmentAnswer/assessmentAnswerFromList");
        view.addObject("assessmentType",  assessmentType);
        Map<Integer,String> assessmentTypeMap=dictionaryBiz.queryDictionaryByParent(DictionaryEnums.FORM_TYPE.getCode());
        //view.addObject("assessmentTypeMap", assessmentTypeMap);
        view.addObject("assessmentTypeName", assessmentTypeMap.get(assessmentType));
        view.addObject("statusMap", JSONObject.toJSONString(CommonStatus.enumMap) );
        return view;
    }

    @RequestMapping("/formListData")
    @ResponseBody
    public String formListData( CommonPage commonPage) {
        UserLoginBean loginBean = (UserLoginBean) this.redisCache.get(Constants.getRedisKey(RedisKeyEnum.USER_LOGIN,(String) SecurityUtils.getSubject().getPrincipal()));
        commonPage.putParam("fromNo",loginBean.getLoginId());
        CommonBizImpl.queryByPage("Assessment",commonPage);
        return commonPage.toSHowPage();
    }


    //查看别人填写的关于我的考核
    @RequestMapping(value = "/toList/{assessmentType}")
    public ModelAndView toList(@PathVariable("assessmentType") Integer assessmentType) {
        ModelAndView view = new ModelAndView("cm/assessmentAnswer/assessmentAnswerToList");
        view.addObject("assessmentType",  assessmentType);
        Map<Integer,String> assessmentTypeMap=dictionaryBiz.queryDictionaryByParent(DictionaryEnums.FORM_TYPE.getCode());
        //view.addObject("assessmentTypeMap", assessmentTypeMap);
        view.addObject("assessmentTypeName", assessmentTypeMap.get(assessmentType));
        view.addObject("statusMap", JSONObject.toJSONString(CommonStatus.enumMap) );
        return view;
    }

    @RequestMapping("/toListData")
    @ResponseBody
    public String toListData( CommonPage commonPage) {
        UserLoginBean loginBean = (UserLoginBean) this.redisCache.get(Constants.getRedisKey(RedisKeyEnum.USER_LOGIN,(String) SecurityUtils.getSubject().getPrincipal()));
        commonPage.putParam("toNo",loginBean.getLoginId());
        CommonBizImpl.queryByPage("Assessment",commonPage);
        return commonPage.toSHowPage();
    }

    @RequestMapping(value = "/getAnswerList")
    public ModelAndView getAnswerList(@RequestParam(value="id")Integer assessmentId){
        ModelAndView view = new ModelAndView("cm/assessmentAnswer/assessmentAnswerList");
        view.addObject("assessmentId",assessmentId);
        return view;

    }


    @RequestMapping("/answerListData")
    @ResponseBody
    public String answerListData( CommonPage commonPage) {
        CommonBizImpl.queryByPage("AssessmentAnswer",commonPage);
        return commonPage.toSHowPage();
    }

}
